// 我的门店
const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    // 支付状态（1待支付、2已支付uu费用、3已全部支付、4商品已支付、5退款中、6已退款）
    payMsg: ["", "待支付", "已支付uu", "已全部支付", "已支付商品", "退款中", "已退款"],
    orderTypeArr: ["", "uu跑腿", "快递", "自提", "扫码订单"],
    statusObj: {
      999: '待提交',
      1: '待取货',
      3: '待收货',
      5: '待评价',
      7: '已完成',
      8: '退款/售后',
      6: '已取消'
    },
    //订单状态0订单创建 1待取货/待发货 2-已取货/已发货 3配送中 4-已送达 5-已完成/待评价 6-已取消 7-已签收/待评价
    tabs: [{
        key: "",
        text: '全部' //uu订单需要打印按钮
      }, {
        key: 1,
        text: '待取货' //uu订单需要打印按钮
      },
      {
        key: 3,
        text: '待收货'
      },
      {
        key: 5,
        text: '待评价'
      },
      {
        key: 7,
        text: '已完成'
      },
      {
        key: 8,
        text: '退款/售后'
      },
      {
        key: 6,
        text: '已取消'
      },

    ],
    status: "",
    tabVal: 'add',
    store: {},
    reqData: {
      size: 10,
      current: 1
    },
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if (options.list) { //(从收款码跳转过来)默认进入订单管理
      this.data.store.sid = options.storeSid
      this.setData({
        tabVal: "list",
        status: options.status || 1
      })

      this.req_list()
    }

  },
  tab_change(e) {
    console.log('tab', e);
    if (this.data.tabVal == e.detail.value) {
      return;
    }
    this.setData({
      tabVal: e.detail.value
    });
    if (e.detail.value == "list") {
      this.data.reqData.current = 1;
      this.req_list()
    }
  },
  update_click() {
    wx.setStorageSync('store', this.data.store);
    wx.navigateTo({
      url: '/pages/store/update',

    })
  },
  order_click(e) {
    wx.navigateTo({
      url: '/pages/store/order/detail?sid=' + e.currentTarget.dataset.sid + "&storeSid=" + this.data.store.sid,
    })
  },
  refund_click(e) {
    wx.showModal({
      content: '您确定要同意此次退款吗？',
      showCancel: true,
      cancelText: "拒绝退款",
      confirmText: "同意退款",
      success: (result) => {

        this.request({
          // url: this.data.host + "/v1/orders/" + e.currentTarget.dataset.sid + "/selfPickupOrderRefund",
          url: this.data.host + "/v1/orders/" + e.currentTarget.dataset.sid + "/review",
          method: "put",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            sid: e.currentTarget.dataset.sid,
            approval: result.confirm ? "3" : "2",
            note: ""
          }

        }, res => {

          this.data.reqData.current = 1;
          this.req_list()
        })
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  cancel_click(e) {
    let ordertype = e.currentTarget.dataset.ordertype || '';
    let url = '';
    let data = {
      goodsOrderSn: e.currentTarget.dataset.sn,
    };
    if (ordertype == 4) {
      url = '/v1/orders/cancel';
      data.reasonSid = '5-dc45e0e47a5111efa17c0242ac4e010b';
    } else {
      url = '/v1/orders/goods-cancel';
      data.storeSid = this.data.store.sid
    }
    wx.showModal({
      content: '是否取消这个订单',
      showCancel: true,
      success: (result) => {
        if (result.confirm) {
          this.request({
            url: this.data.host + url,
            method: "post",
            data: data
          }, res => {
            wx.showToast({
              title: '取消成功',

            })
            let orderList = this.data.orderList;
            orderList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              orderList: orderList
            })

          })
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })

  },
  print_add() {
    wx.navigateTo({
      url: '/pages/store/print?storesSid=' + this.data.store.sid,
    })
  },
  unbind_add() {
    wx.showModal({

      content: '确定要解绑打印机？',

      showCancel: true,
      title: '警告',
      success: (result) => {
        if (result.confirm) {
          this.request({
            url: this.data.host + '/v1/stores/unbinding',
            data: {
              printerId: this.data.store.printerId,
              storesSid: this.data.store.sid,
            },
          }, res => {
            wx.showToast({
              title: '解绑成功',
            })
            this.data.store.printerId = "";
            this.setData({
              store: this.data.store
            })
          })
        }
      },

    })

  },
  onTabChange(e) {
    const {
      value
    } = e.detail;
    this.setData({
      status: value,
    });
    this.data.reqData.current = 1;
    this.req_list();
  },

  print_click(e) {
    console.log('print_click', e);
    // /v1/orders/printer/order/{orderSid}
    this.request({
      url: this.data.host + "/v1/orders/printer/order/" + e.currentTarget.dataset.sid,

    }, res => {
      console.log('订单', res);
      wx.showToast({
        title: '操作成功',

      })
      let orderList = this.data.orderList;
      orderList.splice(e.currentTarget.dataset.index, 1);
      this.setData({
        showEva: false,
        orderList: orderList
      })
    })
  },
  req_list() {
    this.request({
      url: this.data.host + "/v1/orders/store/list",
      data: {
        ...this.data.reqData,
        storeSid: this.data.store.sid,
        orderStatus: this.data.status,
      }
    }, res => {
      // console.log('订单', res);
      res.data.records.map(val => {
        val.goods.map(good => {
          good.thumb = this.get_imgUrl(good.goodsImg)
          // let arr = JSON.parse(good.goodsAttr);
          // let descArr = []
          // arr.map(v => {
          //   v.value.map(v1 => {
          //     descArr.push(v1);
          //   })
          // })
          // good.desc = descArr.join("/");
          good.desc = good.goodsDesc || ""
          // console.log(' good.desc', good.desc);
        })
      })
      this.setData({
        orderList: res.data.records
      })
      this.data.reqData.current++;
    })
  },
  scan_goods() {
    wx.navigateTo({
      url: '/pages/store/order/add?storeSid=' + this.data.store.sid,
    })
  },
  scan_member() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["qrCode", "barCode"],
      success: (result) => {
        console.log("会员", result);
        if (result.result) {
          wx.navigateTo({
            url: '/pages/store/order/add?sid=' + result.result + "&storeSid=" + this.data.store.sid,
          })
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  pay_code(e) {
    wx.navigateTo({
      url: '/pages/pay/Code?sn=' + e.currentTarget.dataset.sn + "&storeSid=" + this.data.store.sid,
    })
  },
  scan_code() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["qrCode", "barCode"],
      success: (result) => {
        // console.log("核销", result);
        if (result.result) {
          wx.navigateTo({
            url: '/pages/pay/Code?sn=' + result.result + "&storeSid=" + this.data.store.sid,
          })
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.request({
      url: this.data.host + "/v1/stores/staff-store"
    }, res => {
      // console.log('门店3', res);
      this.setData({
        store: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})